package com;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.Map;


/**
 * @ClassName Test
 * @Description 类注释
 * @Author 开发者
 * @Date 2023-03-17 12:00
 */
public class WorkDetailDate {
    private static String Cookie = "lastCity=101200100; wd_guid=c0a74f6c-1337-47f0-a1b9-13e47c288a3d; historyState=state; _bl_uid=3Oltwew5sRh6sppFeh6qg2kd142C; __zp_stoken__=59aeeKVRzfUlGM1NwSE8kMRwWLVIuQEAESl9JEXg8PWhyOmQ5Hyo1Ezc2ORwLZCM8RyZKL0BRbGEfMBRdDSoTBkNBAhhTO31TTj1XEihqWRx9QQ4Vfl83EiQiZhR5EhF3BR94TkRbT2QJfkY%3D; __fid=9273c023524a0418cef2aa3b766f52c8; __zp_seo_uuid__=fd472c21-d8ed-4e30-9d33-c8f9048a2181; boss_login_mode=app; __g=-; __c=1677826666; __l=r=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DAupYgTsN2Om5ofC-7nqErcxkLt1C9uaYFrPL5nDi4uukRlklSFKm4x2RG9Wc4Txb%26wd%3D%26eqid%3Df56212e5000c7f1a000000056404853b&l=%2Fwww.zhipin.com%2Fjob_detail%2Fd685b1178bd5c4cd1XV82tq4E1tX.html&s=3&g=&friend_source=0&s=3&friend_source=0; __a=19306706.1677826666..1677826666.57.1.57.57; __zp_sseed__=86sPnhKPeV2bMqQXup8+/ZF4G3Xpq1HyfPQJ8Tl1DUo=; __zp_sname__=feb06034; __zp_sts__=1679034867474";
    private static String user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36";
    private static String url = "";

    public WorkDetailDate(String url) {
        this.url = url;
    }

//    public WorkDetail getWorkDetai() throws IOException {
//        String date = cleanHtml(getHtml(url));
//        WorkDetail workDetail = new WorkDetail();
////        workDetail.setData(date);
//        return workDetail;
//    }

    public static String getHtml(String url) throws IOException {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        String result = "";

        httpClient = HttpClients.createDefault();
//        通过远程连接 连接实例
        HttpGet httpGet = new HttpGet(url);
//      配置请求携带Cookie
        httpGet.setHeader("User-Agent",user_agent);
        httpGet.setHeader("Cookie",Cookie);
        // 设置配置请求参数
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间
                .setConnectionRequestTimeout(35000)// 请求超时时间
                .setSocketTimeout(60000)// 数据读取超时时间
                .build();
        // 为httpGet实例设置配置
        httpGet.setConfig(requestConfig);
        // 执行get请求得到返回对象
        try {
            response = httpClient.execute(httpGet);
            // 通过返回对象获取返回数据
            result = EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    public static String cleanHtml(String html) {
        Document document = Jsoup.parseBodyFragment(html);
        //获取body
        Elements select = document.select("div.job-sec-text").eq(0);
        return select.text();
    }
}
